package com.pz.gulimall.common.utils;

/**
 * 大小驼峰转换
 */
public class PascalCamelCaseTransfer {
    /**
     * 大驼峰转小驼峰
     * @param pascalCase 大驼峰
     * @return 小驼峰
     */
    public static String PascalToCamel(String pascalCase){
        if(pascalCase == null){
            return null;
        }
        String[] split = pascalCase.split("(?=[A-Z])");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < split.length; i++) {
            if(i<split.length-1){
                sb.append(split[i].toLowerCase()).append('_');
            } else {
                sb.append(split[i].toLowerCase());
            }
        }
        return sb.toString();
    }

    /**
     * 小驼峰转大驼峰
     * @param snakeCaseStr 小驼峰
     * @return 大驼峰
     */
    public static String convertToCamelCase(String snakeCaseStr) {
        if (snakeCaseStr == null) {
            return null;
        }
        StringBuilder camelCaseStr = new StringBuilder();
        String[] parts = snakeCaseStr.split("_");
        for (int i = 0; i < parts.length; i++) {
            if(i>0){
                camelCaseStr.append(parts[i].substring(0, 1).toUpperCase());
                camelCaseStr.append(parts[i].substring(1).toLowerCase());
            } else {
                camelCaseStr.append(parts[i]);
            }
        }
        return camelCaseStr.toString();
    }
}
